import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';

class SelectLogic extends GetxController {
  final datas = <String>[].obs;
  final checkedList = <String>[].obs;
  final controller = RefreshController();
  late bool isEdiotr = false;
  @override
  void onInit() {
    super.onInit();
    var arguments = Get.arguments;
    if (arguments != null) {
      checkedList.value = (arguments["strs"] as String).split(',');
      flushData();
    }
    WidgetsBinding.instance.addPostFrameCallback((timeStamp) => testData());
  }

  flushData() {
    if (datas == null || datas.length == 0) {
      return;
    }
    List<String> strs = [];
    for (var i = 0; i < checkedList.length; i++) {
      if (datas.contains(checkedList[i])) {
        strs.add(checkedList[i]);
      }
    }
    checkedList.value = strs;
    flushData();
  }

  load() async {
    testData();
    controller.refreshCompleted();
  }

  void testData() {
    for (var i = 0; i < 20; i++) {
      datas.add("test$i");
    }
  }

  selectAll() {
    // if (isSelectAll) {
    //   for (var info in operableList) {
    //     selectContactsLogic.removeItem(info);
    //   }
    // } else {
    //   for (var info in operableList) {
    //     final isChecked = selectContactsLogic.isChecked(info);
    //     if (!isChecked) {
    //       selectContactsLogic.toggleChecked(info);
    //     }
    //   }
    // }
  }

  bool get isSelectAll {
    return true;
    // if (selectContactsLogic.checkedList.isEmpty) {
    //   return false;
    // } else if (operableList
    //     .every((item) => selectContactsLogic.isChecked(item))) {
    //   return true;
    // } else {
    //   return false;
    // }
  }

  ///选中
  select(String str) {
    if (checkedList.contains(str)) {
      checkedList.remove(str);
    } else {
      checkedList.add(str);
    }
  }

  ///判断是否选择
  isChecked(String str) {
    return checkedList.contains(str);
  }

  ///是否可以选择
  isDefault(String str) {
    return true;
  }

  confirmSelectedList() async {
    var strs = checkedList.join(',');
    if (strs.length > 0) {
      if (strs[0] == ",") {
        strs = strs.substring(1, strs.length);
      }
    }
    Get.back(result: strs);
  }
}
